﻿using System.Collections.Generic;
using System.Linq;

namespace CP_SchoolForChildren_v0._1.Controllers.Fuzzy
{
    class OutliersDetected
    {
        public List<double> Detection(List<double> listData, List<double> temp)
        {
                double Q2 = getMedian(listData);

            var tempQ1 = new List<double>();
            var tempQ3 = new List<double>();
            for (int i = 0; i < listData.Count; i++ )
            {
                if(listData.ElementAt(i) < Q2)
                    tempQ1.Add(listData.ElementAt(i));
                else
                    tempQ3.Add(listData.ElementAt(i));

            }
            double Q1 = getMedian(tempQ1);
            double Q3 = getMedian(tempQ3);
            double IQR = Q3 - Q1;

            double belowR = Q1 - 1.5*IQR;
            double aboveR = Q3 + 1.5*IQR;
            double extremeBelowR = Q1 - 3 * IQR;
            double extremeAboveR = Q3 + 3 * IQR;
            //var sfc = new SFCEntities();
            //var temp = (sfc.Schools.OrderBy(s => s.TuitionFee).Select(s => s.TuitionFee)).Distinct().ToList();
            var tempout = new List<double>();
            for (int i = 0; i < temp.Count; i++)
            {
                if (temp.ElementAt(i) > belowR && temp.ElementAt(i) < aboveR){
                    tempout.Add(temp.ElementAt(i));
                }
            }
            //var newList = new List<double>();
            //for(int i = 0; i < temp.Count; i++)
            //{
            //    if(!newList.Contains(temp[i]))
            //    {
            //        newList.Add(temp[i]);
            //    }
            //}
            return tempout;
        }

        public List<double> Detection(List<double> listData)
        {
            double Q2 = getMedian(listData);

            var tempQ1 = new List<double>();
            var tempQ3 = new List<double>();
            for (int i = 0; i < listData.Count; i++)
            {
                if (listData.ElementAt(i) < Q2)
                    tempQ1.Add(listData.ElementAt(i));
                else
                    tempQ3.Add(listData.ElementAt(i));

            }
            double Q1 = getMedian(tempQ1);
            double Q3 = getMedian(tempQ3);
            double IQR = Q3 - Q1;

            double belowR = Q1 - 1.5 * IQR;
            double aboveR = Q3 + 1.5 * IQR;
            double extremeBelowR = Q1 - 3 * IQR;
            double extremeAboveR = Q3 + 3 * IQR;
            //var sfc = new SFCEntities();
            //var temp = (sfc.Schools.OrderBy(s => s.TuitionFee).Select(s => s.TuitionFee)).Distinct().ToList();
            var tempout = new List<double>();
            for (int i = 0; i < listData.Count; i++)
            {
                if (listData.ElementAt(i) > extremeBelowR && listData.ElementAt(i) < extremeAboveR)
                {
                    tempout.Add(listData.ElementAt(i));
                }
            }
            //var newList = new List<double>();
            //for(int i = 0; i < temp.Count; i++)
            //{
            //    if(!newList.Contains(temp[i]))
            //    {
            //        newList.Add(temp[i]);
            //    }
            //}
            return tempout;
        }

        public double getMedian(List<double> list)
        {
            int lenght = list.Count();
            double median = 0;
            if (lenght % 2 == 1)
                median = list.ElementAt(lenght / 2);
            else
                median = (list.ElementAt(lenght / 2) + list.ElementAt(lenght / 2 - 1)) / 2;
            return median;
        }
    }
    
}
